package com.lorenzogao.lib_view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.Log.e
import android.widget.TextView
import androidx.viewpager.widget.ViewPager
import kotlin.math.abs

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-12 15:49
 *  description:
 */

class ColorTrackTextView : TextView {


    enum class DIRECTION {
        LEFT_TO_RIGHT, RIGHT_TO_LEFT
    }


    private var mOriginColor = Color.BLACK

    private var mChanageColor = Color.RED


    private var mDirection: DIRECTION = DIRECTION.LEFT_TO_RIGHT


    private val mOriginPaint by lazy {
        getPaintByColor(mOriginColor)
    }


    private val mChangePaint by lazy {
        getPaintByColor(mChanageColor)
    }


    private var mCurrentProgress = 0.0f

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val array = context?.obtainStyledAttributes(attrs, R.styleable.ColorTrackTextView)
        array?.apply {
            mOriginColor = getColor(R.styleable.ColorTrackTextView_originColor, mOriginColor)
            mChanageColor = getColor(R.styleable.ColorTrackTextView_changeColor, mChanageColor)
            recycle()
        }


    }


    override fun onDraw(canvas: Canvas) {

        // Step 1 一种文字两种颜色 利用clipRect API 可以裁剪

        //   canvas.clipRect() 裁剪区域


        // Step 1.1 绘制  变色 和不变色


        val middle = mCurrentProgress * width

        //Step 2 实现不同朝向
        if (mDirection == DIRECTION.LEFT_TO_RIGHT) {
            drawText(canvas, mChangePaint, 0f, middle)
            drawText(canvas, mOriginPaint, middle, width.toFloat())
        } else {
            drawText(canvas, mChangePaint, width - middle, width.toFloat())
            drawText(canvas, mOriginPaint, 0f, width - middle)
        }

    }

    /**
     *
     */
    private fun drawText(canvas: Canvas, paint: Paint, start: Float, end: Float) {
        // 根据进度 把中间值算出来
        canvas.save()
        canvas.clipRect(start, 0f, end, height.toFloat())
        val bounds = Rect()
        mOriginPaint.getTextBounds(text.toString(), 0, text.length, bounds)
        val dx = width / 2 - bounds.width() / 2
        val fontMetrics = paint.fontMetrics
        val dy = (abs(fontMetrics.ascent) + fontMetrics.descent) / 2 - fontMetrics.descent
        val baseLine = height / 2 + dy
        canvas.drawText(text.toString(), dx.toFloat(), baseLine, paint)
        canvas.restore()

    }


    fun setDirection(direction: DIRECTION) {
        this.mDirection = direction
    }

    fun setCurrentProgress(progress: Float) {
        this.mCurrentProgress = progress
        postInvalidate()
    }


    private fun getPaintByColor(color: Int) =
        Paint().apply {
            isAntiAlias = true
            this.color = color
            isDither = true // 防抖动
            textSize = this@ColorTrackTextView.textSize
        }

    fun setChanageColor(color: Int) {
        this.mChanageColor = color

    }
    fun setOriginColor(color: Int) {
        this.mOriginColor = color

    }

}